﻿using System.Collections;
using System.Collections.Generic;
using GameFramework.Fsm;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace StarForce {
    public class CardSelectState : BasicCardState
    {
        private Camera camera { get; set; }

        public CardSelectState(Card handler,UiCardParameters parameters) : base(handler,parameters) {
            
        }

        protected override void OnEnter(IFsm<Card> fsm) {
            base.OnEnter(fsm);
            Hander.Hand.OnCardUnSelect += OnCardUnSelect;
            Hander.Hand.OnCardPlay += OnCardPlay;
            Hander.Movement.StopMotion();
            camera = Hander.MainCamera;
            Hander.RotateTo(Vector3.zero,Parameters.RotationSpeed);
            MakeRenderFirst();
            RemoveAllTransparency();
            Hander.Hand.SelectCard(Hander);
        }

        protected override void OnLeave(IFsm<Card> fsm, bool isShutdown) {
            MakeRenderNormal();
            Hander.Hand.OnCardUnSelect -= OnCardUnSelect;
            Hander.Hand.OnCardPlay -= OnCardPlay;
            base.OnLeave(fsm, isShutdown);
        }

        protected override void OnUpdate(IFsm<Card> fsm, float elapseSeconds, float realElapseSeconds) {
            base.OnUpdate(fsm, elapseSeconds, realElapseSeconds);
            FollowCursor();
        }

        private Vector3 WorldPoint() {
            Vector2 mousePoint = Hander.Input.MousePosition;
            Vector3 worldMousePoint = camera.ScreenToWorldPoint(mousePoint);
            return worldMousePoint;
        }

        private void FollowCursor() {
            float myZ = Hander.transform.position.z;
            Vector3 worldPos = WorldPoint();
            worldPos.z = myZ;
            Hander.transform.position = worldPos;
        }

        private void OnCardUnSelect() {
            ChangeState<CardIdleState>(Fsm);
        }

        private void OnCardPlay(Card card) {
            ChangeState<CardDiscardState>(Fsm);
        }
    }
}
